package com.rjwh.dingdong.client.activity.more.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.adapter.MorePointsDetialListAdapter;
import com.rjwh.dingdong.client.bean.localbean.ScoreRecordListData;
import com.rjwh.dingdong.client.fragment.BaseFragment;
import com.rjwh.dingdong.client.mvp.presenter.impl.MorePointsDetialPresenter;
import com.rjwh.dingdong.client.mvp.view.MorePointsDetialView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Kay
 * @ClassName MorePointsDetialFragment
 * @Description
 * @date 2016-9-28
 * @history 2016-9-28 author: description:
 */

public class MorePointsDetialFragment extends BaseFragment implements MorePointsDetialView {


    @BindView(R.id.more_points_detial_list)
    ListView morePointsDetialList;
    @BindView(R.id.more_points_detial_mPullRefreshView)
    SwipyRefreshLayout morePointsDetialMPullRefreshView;
    private int type;
    private boolean isInit;

    private MorePointsDetialPresenter presenter;
    private MorePointsDetialListAdapter adapter;
    private int pageNumber;

    public static MorePointsDetialFragment newInstance(int type) {
        MorePointsDetialFragment fragment = new MorePointsDetialFragment();
        Bundle args = new Bundle();
        args.putInt("type", type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt("type", 1);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.more_points_detial_fragment_layout, container, false);
        ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        presenter = new MorePointsDetialPresenter(getContext(), this);
        if (getUserVisibleHint() && isVisible() && isInit) {
            morePointsDetialMPullRefreshView.doPullRefreshing();//加载数据的方法
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        //判断是否可见 并且是否已经初始化过，以防空指针
        if (isVisibleToUser && isVisible() && isInit) {
            morePointsDetialMPullRefreshView.doPullRefreshing();//加载数据的方法
        } else {
            isInit = false;
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isInit = true;//初始化后 将其设置成true  表示已经初始化控件过了
    }

    private void initView() {
        morePointsDetialMPullRefreshView.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(SwipyRefreshLayoutDirection direction) {
                switch (direction) {
                    case TOP:
                        pageNumber = 0;
                        doGetScoreRecord(true, SwipyRefreshLayoutDirection.TOP);
                        break;

                    case BOTTOM:
                        pageNumber++;
                        doGetScoreRecord(false, SwipyRefreshLayoutDirection.BOTTOM);
                        break;
                    default:
                        break;
                }
            }
        });
        adapter = new MorePointsDetialListAdapter(getContext());
        morePointsDetialList.setAdapter(adapter);
    }

    private void doGetScoreRecord(boolean isRefresh, SwipyRefreshLayoutDirection direction) {
        presenter.doGetMyScoreRecord(pageNumber, type, isRefresh, direction);
    }

    @Override
    public void setDirection(SwipyRefreshLayoutDirection direction) {
        if (morePointsDetialMPullRefreshView != null) {
            morePointsDetialMPullRefreshView.setDirection(direction);
        }
    }

    @Override
    public void setRefreshing(boolean refreshing) {
        if (morePointsDetialMPullRefreshView != null) {
            morePointsDetialMPullRefreshView.setRefreshing(refreshing);
        }
    }

    @Override
    public void loadData(boolean isRefresh, List<ScoreRecordListData> scorelist) {
        if (isRefresh) {
            adapter.clearAll();
        }
        adapter.appendToList(scorelist);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }
}
